using System;
using System.Collections.Generic;

using IBatisNet.Common;
using IBatisNet.DataMapper;

using log4net;

using GymManager.Exception;
using GymManager.Persistence.VO;

namespace GymManager.Persistence.DAO
{
    /// <summary>
    /// Class responsible by getting information from database about stock item
    /// </summary>
    public class StockItemDAO
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(StockItemDAO));

        /// <summary>
        /// Method responsible by getting information about stock item in according to a given description
        /// </summary>
        /// <param name="String">stock item's description</param>
        /// <returns>StockItem - user value object matching with given description</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public StockItem findByDescription(String description)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // stock item value object to be returned
            StockItem stockItemResult = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                stockItemResult = ibatisMapper.QueryForObject<StockItem>("StockItem.findByDescription", description);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving stock item information", ex);
                throw new PersistenceException("Error while retrieving stock item information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return stockItemResult;
        }

        /// <summary>
        /// Method responsible by getting an stock item by its key
        /// </summary>
        /// <param name="Int32">stock item's key</param>
        /// <returns>StockItem - stock item value object</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public StockItem findByKey(Int32 key)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // stock item value object to be returned
            StockItem stockItemResult = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                stockItemResult = ibatisMapper.QueryForObject<StockItem>("StockItem.findByKey", key);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving stock item information", ex);
                throw new PersistenceException("Error while retrieving stock item information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return stockItemResult;
        }


        /// <summary>
        /// Method responsible by getting information all active stock item
        /// </summary>
        /// <returns>IList - list of stock item </returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<StockItem> findAllActive()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // stock item value object list to be returned
            IList<StockItem> stockItemList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                stockItemList = ibatisMapper.QueryForList<StockItem>("StockItem.findAllActive", null);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving stock item information", ex);
                throw new PersistenceException("Error while retrieving stock item information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return stockItemList;
        }

        /// <summary>
        /// Method responsible by adding a new stock item to database
        /// </summary>
        /// <param name="StockItem">stock item information to be saved</param>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public void create(StockItem stockItemVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                ibatisMapper.Insert("StockItem.insert", stockItemVO);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while saving stock item information", ex);
                throw new PersistenceException("Error while saving stock item information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

        /// <summary>
        /// Method responsible by updating an stock item to database
        /// </summary>
        /// <param name="StockItem">stock item information to be saved</param>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public void update(StockItem stockItemVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                ibatisMapper.Update("StockItem.update", stockItemVO);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while saving stock item information", ex);
                throw new PersistenceException("Error while saving stock item information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

        /// <summary>
        /// Method responsible by finding stock item using any of its properties
        /// </summary>
        /// <param name="StockItem">stock item vo filter</param>
        /// <returns>IList - list of stock item retrieved in according to filter</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<StockItem> findByAny(StockItem stockItemVOFilter)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list of stock item found
            IList<StockItem> stockItemList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                stockItemList = ibatisMapper.QueryForList<StockItem>("StockItem.findByAny", stockItemVOFilter);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving stock item information", ex);
                throw new PersistenceException("Error while retrieving stock item information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return stockItemList;
        }

        /// <summary>
        /// Method responsible by removing an stock item from database
        /// </summary>
        /// <param name="Int32">stock item identifier</param>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public void remove(Int32 stockItemId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                ibatisMapper.Delete("StockItem.delete", stockItemId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while removing stock item information", ex);
                throw new PersistenceException("Error while removing stock item information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }
     }
}
